# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# 建立者:        黄周云  
# Name:         test05
# Description:  例10.5
# Author:       ASUS
# Date:         2019/3/18
#-------------------------------------------------------------------------------


#定义一个从list继承的类
class CounterList(list):
    def __init__(self,*args):
        super(CounterList, self).__init__(*args)
        self.counter = 0
    def __getitem__(self, index):
        self.counter += 1
        return super(CounterList, self).__getitem__(index)

c = CounterList(range(10))
print(c)
c.reverse()
print(c)
del c[2:7]
print(c.counter)
print(c[1]+c[2])
print(c.counter)


#定义一个从dict继承的类
class CounterDict(dict):
    def __init__(self,*args):
        super().__init__(*args)
        self.counter = 0
    def __getitem__(self,key):
        self.counter += 1
        return super(CounterDict, self).__getitem__(key)

d = CounterDict({'name':'Bill'})
print(d['name'])
print(d.get('age'))
d['age'] = 30
print(d['age'])
print(d.counter)


#定义一个从str继承的类
class MultiString(str):
    def __new__(cls, *args,sep = ' '):
        s = ''
        for arg in args:
            s +=arg + sep
        index = - len(sep)
        if index == 0:
            index = len(s)
        return str.__new__(cls,s[:index])
    def __init__(self,*args,sep = ' '):
        pass

cs1 = MultiString('a','b','c')
cs2 = MultiString('a','b','c',sep = ',')
cs3 = MultiString('a','b','c',sep = '')
print('['+cs1+']')
print('['+cs2+']')
print('['+cs3+']')

